<template>
	<!-- 解决页面刷新闪烁的问题 -->
	<view v-if="goods_info.goods_name">
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true"
			indicator-color="#EEEEEE" indicator-active-color="#FF6633">
			<swiper-item v-for="(item,i) in goods_info.pics" :key="i">
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区 -->
		<view class="goods-info-box">
			<!-- 商品的价格 -->
			<view class="price">￥{{getprice[0]}}.{{getprice[1]}}</view>
			<!-- 商品的主体区域 -->
			<view class="goods-info-body">
				<!-- 商品的名字 -->
				<view class="goods-name">{{goods_info.goods_name}}</view>
				<!-- 收藏 -->
				<view class="collection">
					<uni-icons type="star" size="20" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="freight">快递：免运费</view>
		</view>
		<!-- h5代码渲染到微信小程序 -->
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<!-- 底部商品导航区域 -->
		<view class="goods_nav">
			<uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	// 按需导入 mapMutations 这个辅助方法
	import {
		mapMutations,
		mapGetters
	} from 'vuex'
	import {
		formatValue
	} from '@/formatValue/formatValue.js'
	export default {
		// 计算属性
		computed: {
			...mapGetters('m_cart', ['total']),
			getprice() {
				return `${formatValue(this.goods_info.goods_price, 'split')}`.split(',')
			}
		},
		// 侦听器
		watch: {
			// 页面首次加载完毕后，不会调用这个侦听器(怎么解决第一次进来的时候数量正确?)
			// total(newVal) {
			// 	const findResult = this.options.find(x => x.text === '购物车')
			// 	if (findResult) {
			// 		findResult.info = newVal
			// 	}
			// }
			// 优化如下:
			total: {
				handler(newVal) {
					const findResult = this.options.find(x => x.text === '购物车')
					// console.log(findResult);
					if (findResult) {
						findResult.info = newVal
					}
				},
				immediate: true
			}
		},
		data() {
			return {
				// 商品详情对象
				goods_info: {
					pics: [],
					goods_price: 0,
					goods_name: '',
					goods_introduce: ''
				},
				options: [{
					icon: 'headphones',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺'
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		onLoad(options) {
			// 调用商品详情方法
			this.getGoodsDetail(options.goods_id)
		},
		methods: {
			...mapMutations('m_cart', ['addToCart']),
			// 定义商品详情方法
			async getGoodsDetail(goods_id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;"').replace(/webp/g, 'jpg')
				this.goods_info = res.message
			},
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.goods_info.pics.map(x => x.pics_big)
				})
			},
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				} else if (e.content.text === '店铺') {
					uni.navigateTo({
						url: '/subpkg/goods_shop/goods_shop'
					})
				} else {
					uni.showToast({
						title: '功能尚未开放',
						icon: 'none'
					})
				}
			},
			buttonClick(e) {
				// 判断是否点击了"加入购物车按钮"
				if (e.content.text === '加入购物车') {
					// 组织商品的信息对象
					const goods = {
						goods_id: this.goods_info.goods_id,
						goods_name: this.goods_info.goods_name,
						goods_price: this.goods_info.goods_price.toFixed(2),
						goods_count: 1,
						goods_small_logo: this.goods_info.goods_small_logo,
						goods_state: true
					}
					// 调用 addToCart 方法
					this.addToCart(goods)
				}
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 620rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.goods-info-box {
		padding: 20rpx;
		padding-right: 0;

		// 商品的价格
		.price {
			color: #C00000;
			font-size: 36rpx;
			margin: 20rpx 0;
		}

		// 商品的主体区域
		.goods-info-body {
			display: flex;
			justify-content: space-between;

			// 商品的名字
			.goods-name {
				font-size: 26rpx;
				font-weight: 600;
				margin-right: 20rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				/* 将对象作为弹性伸缩盒子模型显示 */
				display: -webkit-box;
				/* 限制在一个块元素显示的文本的行数 */
				-webkit-line-clamp: 3;
				/* 设置或检索伸缩盒对象的子元素的排列方式 */
				-webkit-box-orient: vertical;
			}

			// 收藏
			.collection {
				width: 240rpx;
				font-size: 24rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				border-left: 2rpx solid #efefef;
				color: gray;
			}
		}

		// 运费
		.freight {
			font-size: 24rpx;
			color: gray;
			margin: 20rpx 0;
		}
	}

	.goods_nav {
		position: sticky;
		bottom: 0;
		width: 100%;
	}
</style>
